package com.adcc.loadsheet.billing.util;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeUtil {
    private static final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dateFormatter1 = DateTimeFormat.forPattern("yyyy年MM月dd日");
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter dateHmsFormatter = DateTimeFormat.forPattern("HH:mm:ss");

    //获取带时分秒Date类型的当前时间
    public static Date getNowDate() {
        return new LocalDateTime(DateTimeZone.forID("Asia/Shanghai")).toDate();
    }

    //将Date转成yyyy-MM-dd格式的字符串
    public static String toDateString(Date date) {
        if (date == null) {
            return null;
        }
        return new LocalDateTime(date, DateTimeZone.forID("Asia/Shanghai")).toString(dateFormatter);
    }

    //将Date转成HH:mm:ss格式的字符串
    public static String toDateTimeString(Date date) {
        if (date == null) {
            return null;
        }
        return new LocalDateTime(date, DateTimeZone.forID("Asia/Shanghai")).toString(dateTimeFormatter);
    }

    //将String转成yyyy-MM-dd HH:mm:ss格式的Date
    public static Date toDateTimeForString(String time) {
        if (Strings.isNullOrEmpty(time) || time.length() != 19) {
            return null;
        }
        return LocalDateTime.parse(time, dateTimeFormatter).toDate();
    }

    //获取两个日期间相差的天数
    public static Integer getBetweenDate(String startDate,String endDate){
        LocalDate startTime = LocalDate.parse(startDate, dateFormatter);
        LocalDate endTime = LocalDate.parse(endDate, dateFormatter);
        return Days.daysBetween(startTime,endTime).getDays();
    }

    //格式化日期
    public static String getDateFormatter(String dateVal) {
        LocalDateTime date = LocalDateTime.parse(dateVal, dateFormatter);
        return date.toString(dateFormatter1);
    }

    //获取两个日期之间的所有日期集合（正序排列）
    public static List<String> getBetweenDates(String startDate, String endDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> betweenList = Lists.newArrayList();

        try{
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(format.parse(startDate));
            startDay.add(Calendar.DATE, -1);

            while(true){
                startDay.add(Calendar.DATE, 1);
                Date newDate = startDay.getTime();
                String newend=format.format(newDate);
                betweenList.add(newend);
                if(endDate.equals(newend)){
                    break;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return betweenList;
    }

    //根据月份获取当年度一月
    public static Integer getYear(String month) {
        DateTimeFormatter monthFormatter = DateTimeFormat.forPattern("yyyy-MM");
        Date d = LocalDateTime.parse(month, monthFormatter).toDate();
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.YEAR);
    }

    //获取两个月份之间的所有月份集合（正序排列，格式：“2020年01月”）
    public static List<String> getBetweenMonths(String startMonth, String endMonth) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        List<String> betweenList = Lists.newArrayList();
        try{
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(format.parse(startMonth));
            startDay.add(Calendar.MONTH, -1);

            while(true){
                startDay.add(Calendar.MONTH, 1);
                Date newDate = startDay.getTime();
                String newend = format.format(newDate);
//                betweenList.add(startDay.get(Calendar.YEAR) + "年" + (startDay.get(Calendar.MONTH)+1) + "月");
                betweenList.add(newend);
                if(endMonth.equals(newend)){
                    break;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return betweenList;
    }

    //取日期所在月份的最后一天
    public static String getLastDay(String day) {
        String res = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(day));
            calendar.add(Calendar.MONTH, 1); // 加一个月
            calendar.set(Calendar.DATE, 1); // 设置为该月第一天
            calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天
            Date date = calendar.getTime();
            res = format.format(date);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    //取日期所在月份的最后一天
    public static Date getLastDate(String day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(day));
            calendar.add(Calendar.MONTH, 1); // 加一个月
            calendar.set(Calendar.DATE, 1); // 设置为该月第一天
            calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天
            return calendar.getTime();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取上个月第一天的日期
    public static String getLastMonthFirstDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return format.format(cal_1.getTime());
    }

    //获取上个月最后一天的日期
    public static String getLastMonthLastDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        return format.format(cale.getTime());
    }

    //获取上个月月份
    public static String getLastMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);      //当前月的上个月  （-1改为1的话，为取当前月的下个月）
        return format.format(cal.getTime());
    }
}